package com.example.leo.mycharmingstudent.project.maintabfragment.subpagefragment.wxpublic;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.leo.mycharmingstudent.R;
import com.example.leo.mycharmingstudent.project.MainActivity;
import com.example.leo.mycharmingstudent.project.constant.ApiInterface;
import com.example.leo.mycharmingstudent.project.maintabfragment.subpagefragment.entity.WXArticleEntity;
import com.example.leo.mycharmingstudent.project.util.OkHttpUtil;
import com.flyco.tablayout.SlidingTabLayout;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * Created by Leo on 2020/9/28.
 */

public class PublicFragment extends Fragment {
    private SlidingTabLayout mTabLayout;
    private ViewPager mViewPager;

    private List<PublicDetailFragment> mFragments = new ArrayList<>();

    public static PublicFragment newInstance(String name) {
        PublicFragment publicFragment = new PublicFragment();
        Bundle bundle = new Bundle();
        bundle.putString(MainActivity.FRAGMENT_NAME, name);
        publicFragment.setArguments(bundle);
        return publicFragment;
    }

    /**
     * 呈现给用户最美好的一面
     * 通俗来讲，叫创建View
     *
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_public, container, false);
        mTabLayout = inflate.findViewById(R.id.wx_detail_tab_layout);
        mViewPager = inflate.findViewById(R.id.wx_detail_viewpager);
        return inflate;
    }

    /**
     * 初始化数据,例如网络请求
     *
     * @param savedInstanceState
     */
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        requestAuthor();
    }

    private void requestAuthor() {
        OkHttpUtil.enqueue(ApiInterface.WX_ARTICLE, new OkHttpUtil.MainThreadCallBack() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, String result) {
                Gson gson = new Gson();
                WXArticleEntity wxArticleEntity = gson.fromJson(result, WXArticleEntity.class);
                if (wxArticleEntity.getErrorCode() == 0) {
                    mFragments.clear();

                    List<WXArticleEntity.DataBean> data = wxArticleEntity.getData();
                    int size = data.size();
                    for (int i = 0; i < size; i++) {
                        WXArticleEntity.DataBean dataBean = data.get(i);
                        PublicDetailFragment detailFragment = PublicDetailFragment.newInstance(dataBean.getId(), dataBean.getName());
                        mFragments.add(detailFragment);
                    }
                    initViewPagerAndTabLayout(data);
                }
            }
        });
    }

    private void initViewPagerAndTabLayout(final List<WXArticleEntity.DataBean> wxAuthors) {
        mViewPager.setAdapter(new FragmentStatePagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments == null? 0 : mFragments.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return wxAuthors.get(position).getName();
            }
        });
        mTabLayout.setViewPager(mViewPager);
    }
}
